package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.example.common.R;
import org.example.entity.DTO.RecordDto;
import org.example.entity.VisitorRecords;
import org.example.service.VisitorRecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * TODO 类描述
 *
 * @author shaoxuan.
 * @date 2024/1/10 16:02
 */
@RestController
@Api(tags = "访客记录管理")
public class VisitorController {
    @Autowired
    private VisitorRecordsService service;

    @ApiOperation("查看所有访客记录")
    @GetMapping("/getAll")
    public R<List<VisitorRecords>> getAllRecords(){
        LambdaQueryWrapper<VisitorRecords> wrapper =
                new LambdaQueryWrapper<VisitorRecords>().eq(VisitorRecords::getIsDelete, 0);

        List<VisitorRecords> list = service.list(wrapper);
        return R.success(list);
    }

    @ApiOperation("查看已撤销访客记录")
    @GetMapping("/getRemoved")
    public R<List<VisitorRecords>> getAllRemoveRecords(){
        LambdaQueryWrapper<VisitorRecords> wrapper =
                new LambdaQueryWrapper<VisitorRecords>().eq(VisitorRecords::getIsDelete, 1);

        List<VisitorRecords> list = service.list(wrapper);
        return R.success(list);
    }

    @ApiOperation("查看未审批访客记录")
    @GetMapping("/getUnApproved")
    public R<List<VisitorRecords>> getUnApproved(){
        LambdaQueryWrapper<VisitorRecords> wrapper =
                new LambdaQueryWrapper<VisitorRecords>().eq(VisitorRecords::getApprove, 0);

        List<VisitorRecords> list = service.list(wrapper);
        return R.success(list);
    }



    @PostMapping("/addRecord")
    @ApiOperation("添加访客申请")
    public R<VisitorRecords> addRecord(@RequestBody VisitorRecords records){
        boolean save = service.save(records);
        if (save){
            return R.success(records).setMsg("添加成功");
        }else {
            return R.error("添加失败");
        }
    }

    @PutMapping("/quashRecord/{id}")
    @ApiOperation("撤销访客申请")
    public R<VisitorRecords> quashRecord(@PathVariable("id") Integer id){
        VisitorRecords visitorRecords = new VisitorRecords();
        visitorRecords.setIsDelete(1);
        visitorRecords.setId(id);
        boolean res = service.updateById(visitorRecords);
        if (res){
            return R.success(null).setMsg("撤销成功");
        }else {
            return R.error("撤销失败");
        }
    }


    @PutMapping("/approve/{id}")
    @ApiOperation("审批访客申请")
    public R<VisitorRecords> approveRecord(@PathVariable("id") Integer id){
        VisitorRecords visitorRecords = new VisitorRecords();
        visitorRecords.setApprove(1);
        visitorRecords.setId(id);
        boolean res = service.updateById(visitorRecords);
        if (res){
            return R.success(null).setMsg("审批通过");
        }else {
            return R.error("审批失败");
        }
    }


    @ApiOperation("根据身份证查询所有访客记录")
    @GetMapping("/getRecordsById/{id}")
    public R<List<VisitorRecords>> getRecordsById(@PathVariable("id") String id){
        LambdaQueryWrapper<VisitorRecords> wrapper =
                new LambdaQueryWrapper<VisitorRecords>().eq(VisitorRecords::getIdCard, id);

        List<VisitorRecords> list = service.list(wrapper);
        return R.success(list);
    }



    @ApiOperation("获取每日访客人数")
    @GetMapping("/getRecords")
    public R<List<RecordDto>> getRecords(){
        List<RecordDto> records = service.getRecords();
        return R.success(records);
    }
}
